<?php


namespace App\Http\Repositories\UserCenter;


use App\Models\UserModels\RulesModel;
use App\Http\Repositories\BaseRepository as Repository;
use Illuminate\Support\Facades\Cache;

class RulesRepository extends  Repository
{
    const REDIS_KEY = "admin:rule:trees";
    private $rules;
    public function __construct(RulesModel $rules)
    {
        $this->rules = $rules;
    }

    /**
     * 详情
     * @param $data
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function getRulesInfo($data)
    {
        return $this->rules->getInfo($data);
    }

    /**
     * 添加
     * @param $data
     * @return mixed
     */
    public function addRules($data)
    {
        $data = $this->rules->setUserInfo($data);
        return $this->rules->addItem($data);
    }

    /**
     * 删除
     * @param $data
     * @return array
     */
    public function delRules($data)
    {
        return $this->rules->deleteItem($data);
    }

    /**
     * 编辑
     * @param $data
     * @return mixed
     */
    public function editRules($data)
    {
        $where['id'] = $data['id'];

        $this->rules->editItem($where, $data);

        return [];
    }

    /**
     * 获取列表
     * @param $data
     * @return array
     */
    public function getRulesList($data)
    {
        $where = [];
        if ($data['operate'] == 1) {
            isset($data['menu']) && $where['menu'] = ['like', '%'.$data['menu'].'%'];
            isset($data['path']) && $where['path'] = ['like', $data['path'].'%'];
            isset($data['status']) && $where['status'] = $data['status'];
            isset($data['is_show']) && $where['is_show'] = $data['is_show'];
            isset($data['create_time']) && $where['create_time'] = ['between', $data['create_time']];
            isset($data['update_time']) && $where['update_time'] = ['between', $data['update_time']];

            return $this->rules->getListByPage($where);
        }

        $field = "*,id as value,rule as label,parent_id,ids_url";
//        $where['status'] = $this->rules::TRUE;
        $res = $this->rules->getList($where, $field)->toArray();
        $res = $this->generateTree($res);

        return $res;
    }

}
